package com.kkb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.DoctorMapper;
import com.kkb.pojo.Doctor;
import com.kkb.pojo.DoctorExample;
import com.kkb.vo.QueryVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class DoctorService {
    @Resource
    private DoctorMapper doctorMapper;
    //查询所有医生信息
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public PageInfo<Doctor> queryByPage(Integer pageNum, Integer pageSize, QueryVO vo){
        DoctorExample example = new DoctorExample();
        DoctorExample.Criteria criteria = example.createCriteria();
        if (vo != null){
            if (vo.getD_id()!=null && vo.getD_id()>0){
                criteria.andD_idEqualTo(vo.getD_id());
            }
            if (vo.getD_name()!=null && !"".equals(vo.getD_name().trim())){
                criteria.andD_nameEqualTo(vo.getD_name().trim());
            }
            if (vo.getD_keshi()!=null && !"".equals(vo.getD_keshi().trim())){
                criteria.andD_keshiEqualTo(vo.getD_keshi());
            }
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Doctor> list = doctorMapper.selectByExample(example);
        for (Doctor doctor : list) {
            System.out.println(doctor);
        }
        return new PageInfo<>(list);
    }
    //添加医生
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Integer addDoctor(Doctor doctor){
        if (doctor != null){
            doctor.setD_inTime(new Date());
            doctor.setD_state(0);
        }
        return doctorMapper.insertSelective(doctor);
    }
    //查询单个医生信息
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public Doctor queryById(Integer d_id){
        return doctorMapper.selectByPrimaryKey(d_id);
    }
    //修改医生信息
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Integer update(Doctor doctor){
        return doctorMapper.updateByPrimaryKeySelective(doctor);
    }
}
